package com.aspirecn.core.object;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import com.aspirecn.core.commons.StringUtils;

public class MatchUACondition implements Comparable<MatchUACondition> {

	public int getDeviceid() {
		return deviceid;
	}

	public void setDeviceid(int deviceid) {
		this.deviceid = deviceid;
	}

	public String[] getKeywords() {
		return keywords;
	}

	public void setKeywords(String[] keywords) {
		this.keywords = keywords;
	}

	private int deviceid;
	private String[] keywords;

	public String getUa() {
		return ua;
	}

	public void setUa(String ua) {
		this.ua = ua;
	}

	public String getOsua() {
		return osua;
	}

	public void setOsua(String osua) {
		this.osua = osua;
	}

	public String getGwua() {
		return gwua;
	}

	public void setGwua(String gwua) {
		this.gwua = gwua;
	}

	private String ua;
	private String osua;
	private String gwua;

	public void addKeyword(String keyword) {
		
		if (StringUtils.isNotBlank(keyword)
				&& !StringUtils.equals(keyword, "%")
				&& !StringUtils.equals(keyword, "?")
				&& !ArrayUtils.contains(keywords, StringUtils.lowerCase(keyword) )) {
			keywords = ArrayUtils.add(keywords, StringUtils.lowerCase(keyword) );
		}
	}

	public int compareTo(MatchUACondition arg0) {

		if (ArrayUtils.getLength(this.keywords) > ArrayUtils
				.getLength(arg0.keywords)) {
			return -1;
		} else if (ArrayUtils.getLength(this.keywords) < ArrayUtils
				.getLength(arg0.keywords)) {
			return 1;
		} else// ==
		{
			String s1 = ArrayUtils.toString(this.keywords);
			String s2 = ArrayUtils.toString(arg0.keywords);

			if (StringUtils.length(s1) > StringUtils.length(s2)) {
				return -1;
			} else {
				return 1;
			}
		}
	}

	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}
}